package com.example.wanandroid.ui.adapter;

import static androidx.core.app.ActivityCompat.recreate;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.wanandroid.R;
import com.example.wanandroid.app.WanAndroidApp;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.main.collect.FeedArticleData;
import com.example.wanandroid.core.dao.HistoryData;
import com.example.wanandroid.core.dao.HistoryDataDao;
import com.example.wanandroid.core.db.DbHelperImpl;
import com.example.wanandroid.ui.activity.ProjectDetailActivity;
import com.example.wanandroid.ui.activity.SearchActivity;
import com.example.wanandroid.ui.activity.SearchResultActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class HistoryDataListAdapter extends RecyclerView.Adapter<HistoryDataListAdapter.MyViewHoder> {

    private String TAG = "HistoryDataListAdapter";
    List<HistoryData> dataList = new ArrayList<>();
    SearchActivity context;
    private LayoutInflater mLayoutInflater;
    Object lock = new Object();
    DbHelperImpl dbHelper;

    public HistoryDataListAdapter(Context context){
        this.context= (SearchActivity) context;
        mLayoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public HistoryDataListAdapter.MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mLayoutInflater.getContext(), R.layout.item_history_search_list, null);
        HistoryDataListAdapter.MyViewHoder myViewHoder = new HistoryDataListAdapter.MyViewHoder(view);
        return myViewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryDataListAdapter.MyViewHoder holder, int position) {
        HistoryData data = dataList.get(position);
        holder.mData.setText(data.getData());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mData;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            //数据库操作，获取历史数据操作对象，然后插入搜索历史并获取历史信息，渲染进RecyclerView中展示
            HistoryDataDao historyDataDao = WanAndroidApp.getInstance().getDaoSession().getHistoryDataDao();
            dbHelper = new DbHelperImpl(historyDataDao);
            mData = itemView.findViewById(R.id.search_data);
            CardView mCard=itemView.findViewById(R.id.search_card);
            mCard.setOnClickListener(v->{
                dbHelper.addHistoryData(mData.getText().toString());
                context.recreate();
                EditText editText = context.findViewById(R.id.search_edit);
                Intent intent=new Intent(itemView.getContext(), SearchResultActivity.class);
                intent.putExtra("input",mData.getText());
                editText.setText(mData.getText());
                itemView.getContext().startActivity(intent);
            });
        }
    }
    public void refreshData(List<HistoryData> list){
        synchronized (lock){
            this.dataList = list;
            Collections.reverse(this.dataList);
            notifyDataSetChanged();
        }
    }

    public void addData(BaseData data){
//        synchronized (lock){
//            this.dataList.add(data);
//            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
//            this.dataList = list;
//            notifyItemInserted(this.dataList.size()-1);
//        }
    }

    public void addDataList(List<BaseData> data){
//        synchronized (lock){
//            this.dataList.addAll(data);
//            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
//            this.dataList = list;
//            notifyDataSetChanged();
//        }
    }
}
